<?php 
/**
 * 数据库自增自减
 **/
namespace App\Tools;

use App\Models\Shop;
use App\Models\Order;
use App\Models\OrderDiscount;
use App\Models\ShopCheapen;
use App\Models\UserCoupon;

class Activity{
    //商家活动(订单id，优惠类型（1单单立减2优惠券），$coupon_id(优惠券id))
    public function activity($order_id=false,$type=false,$coupon_id=false){
        try{
        switch($type){
            case 1://单单立减
                if($order_id==false || $type==false){
                    return false;
                }
                //验证订单
                $order = Order::where('order_type',3)->where('order_status',1)->find($order_id);
                if(!$order){
                    return false;
                }
                //是否商家本人
                $shop = Shop::find($order->shop_id);
                if(!$shop){
                    return false;
                }
                if($shop->user_id == $order->user_id){
                    return false;
                }
                //非用户不给优惠
                if($order->user_id==-1){
                    return false;
                }
                //是否优惠过
                $orderdisount=OrderDiscount::where('order_id',$order_id)->first();
                if($orderdisount){
                    return false;
                }
                //用户一天是否使用过两次单单立减
                $orderdisount2=OrderDiscount::where('user_id',$order->user_id)
                     ->where('type',1)
                     ->whereBetween('created_at',[date("Y-m-d"),date("Y-m-d H:i:s")])
                     ->get();
                if(count($orderdisount2)>1){
                    return false;
                }
                //获取优惠
                $shopcheapen = ShopCheapen::where('shop_id',$order->shop_id)->first();
                if(!$shopcheapen){
                    return false;
                }
                //计算优惠
                $randdiscount =  rand(1,(1-$shopcheapen->discount)*100)/100;
                $amount = $order->order_amount*$randdiscount;
                if($amount > $shopcheapen->capping){//封顶
                    $amount = $order->order_amount-$shopcheapen->capping;
                }else{//没封顶
                    $amount = $order->order_amount-$amount;
                }
                //优惠人id
                $user_id = $order->user_id;
                //原来价格
                $originalprice =  $order->order_amount;
                $order->order_amount=$amount;
                $order->save();
                OrderDiscount::create([
                    'order_id'=>$order_id,
                    'user_id'=>$user_id,
                    'type'=>1,//单单立减
                    'coupon_id'=>$shopcheapen->id,
                    'who_subsidy'=>-1,//平台
                    'before_price'=>$originalprice,
                    'now_price'=>$amount
                ]);
                return false;
                break;
            case 2://优惠卡券

                if($order_id==false || $type==false || $coupon_id==false){
                    return false;
                }else{

                    //判断订单是否有优惠
                    $orderdisount=OrderDiscount::where('order_id',$order_id)->first();
                    if($orderdisount){
                        return false;
                    }
                }
                //验证订单
                $order = Order::whereIn('order_type',[0,1])->find($order_id);
                if(!$order){
                    return false;
                }
                //是否使用过该优惠券
                $orderdisount = OrderDiscount::where('type',2)->where('order_id',$order->id)->where('coupon_id',$coupon_id)->first();
                if($orderdisount){
                    return false;
                }
                //获取优惠劵
                $usercoupon = UserCoupon::where('coupon_id',$coupon_id)->where('user_id',$order->user_id)->where('shop_id',$order->shop_id)->where('user_id',$order->user_id)->first();
                if(!$usercoupon){
                    return false;
                }else{//获取优惠券处理逻辑
                    if(!$usercoupon->coupondetail){
                        return false;
                    }

                }
                $coupondetail=$usercoupon->coupondetail;
                //未启用//优惠券过期
                if(strtotime($coupondetail->coupon_end_time)<time() || $coupondetail->status!=1) {
                    return false;
                }
                //优惠券是否通用
                if($coupondetail->shop_id == -1){//通用
                    //是否商家本人
                    $shop = Shop::find($order->shop_id);
                    if(!$shop){
                        return false;
                    }
                    if($shop->user_id == $order->user_id){
                        return false;
                    }
                }else{//非通用
                    //是否商家优惠券
                    if($order->shop_id != $coupondetail->shop_id){
                        return false;
                    }
                }
                switch ($order->order_type){
                    case 0://外卖
                        $amount = $order->order_amount;
                        if($amount<$coupondetail->spend_money){
                            return false;
                        }
                        $originalprice = $order->order_amount;
                        $user_id = $order->user_id;
                        //计算价格
                        $amount = $amount-$coupondetail->coupon_money;
                        $order->order_amount = $amount;
                        if($coupondetail->shop_id != -1){
                            $order->order_price = $order->order_price-$coupondetail->coupon_money;
                        }
                        $order->save();
                        break;
                    case 1://预定
                        return false;
                        //是否买单
                        if($order->order_status != 3){
                            return false;
                        }
                        $amount = $order->order_price;
                        if($amount<$coupondetail->spend_money){
                            return false;
                        }
                        $originalprice = $order->order_price;
                        //计算价格
                        $amount =  $amount-$coupondetail->coupon_money;
                        if($amount-$order->order_amount <= 0){
                            return false;
                        }
                        //不能使用通用劵
                        if($coupondetail->shop_id ==1){
                            return false;
                        }
                        $order->order_price = $amount;
                        $order->save();
                        break;
                    default:
                        return false;
                        break;
                }
                //做下记录
                OrderDiscount::create([
                    'order_id'=>$order_id,
                    'user_id' => $user_id,
                    'type'=>2,//优惠券
                    'coupon_id'=>$coupon_id,
                    'who_subsidy'=>$coupondetail->shop_id,//平台
                    'before_price'=>$originalprice,
                    'now_price'=>$amount
                ]);
                return true;
                break;
            default:
                return false;
                break;

        }

        }catch (\Exception $e){
            return false;
        }
    }


}

